home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / TypeValidator.java < prev    next >
Text File  |  1998-11-03  |  6KB  |  261 lines

  1. package com.symantec.itools.tools.archive;
  2.  
  3.  
  4. import java.io.File;
  5. import com.symantec.itools.io.FileSystem;
  6. import com.symantec.itools.lang.Classpath;
  7.  
  8.  
  9. /**
  10.  * @author Symantec Internet Tools Division
  11.  * @version 1.0
  12.  * @since VCafe 3.0
  13.  */
  14.  
  15. public abstract class TypeValidator
  16. {
  17.     /**
  18.      * @since VCafe 3.0
  19.      */
  20.     protected Options options;
  21.  
  22.     /**
  23.      * @since VCafe 3.0
  24.      */
  25.     protected boolean canceled;
  26.  
  27.     protected TypeValidator(Options opts)
  28.     {
  29.         options = opts;
  30.     }
  31.  
  32.     /**
  33.      * @param errorMsg TODO
  34.      * @since VCafe 3.0
  35.      */
  36.  
  37.     public abstract boolean validate(StringBuffer errorMsg);
  38.  
  39.     /**
  40.      * @param file TODO
  41.      * @param errorMsg TODO
  42.      * @since VCafe 3.0
  43.      */
  44.  
  45.     public boolean validate(boolean file, StringBuffer errorMsg)
  46.     {
  47.         if(file)
  48.         {
  49.             if(!(validateOutputFile(errorMsg)))
  50.             {
  51.                 return (false);
  52.             }
  53.         }
  54.         else
  55.         {
  56.             if(!(validateOutputDirectory(errorMsg)))
  57.             {
  58.                 return (false);
  59.             }
  60.         }
  61.  
  62.         if(!(validateInput(errorMsg)))
  63.         {
  64.             return (false);
  65.         }
  66.  
  67.         if(!(validateFiles(errorMsg)))
  68.         {
  69.             return (false);
  70.         }
  71.  
  72. // THIS MAKES US _WAY_ TOO SLOW - it is handled in the Dir copy now
  73. //        if(!(validateEntries(errorMsg)))
  74. //        {
  75. //            return (false);
  76. //        }
  77.  
  78.         return (true);
  79.     }
  80.  
  81.     /**
  82.      * @param errorMsg TODO
  83.      * @since VCafe 3.0
  84.      */
  85.  
  86.     protected boolean validateOutputFile(StringBuffer errorMsg)
  87.     {
  88.         String fileName;
  89.         File   file;
  90.  
  91.         fileName = options.getOutputLocation();
  92.  
  93.         if(fileName == null)
  94.         {
  95.             errorMsg.append("You must specify an output file");
  96.             return (false);
  97.         }
  98.  
  99.         if(!(fileName.toLowerCase().endsWith('.' + options.getType())))
  100.         {
  101.             fileName += '.' + options.getType();
  102.             options.setOutputLocation(fileName);
  103.         }
  104.  
  105.         file = new File(FileSystem.getCanonicalPath(fileName, true, false));
  106.  
  107.         if(file.exists())
  108.         {
  109.             if(file.isDirectory())
  110.             {
  111.                 errorMsg.append(FileSystem.getCanonicalPath(file, true)).append(" is a directory");
  112.                 return (false);
  113.             }
  114.  
  115.             if(!(options.isOverwrite()))
  116.             {
  117.                 errorMsg.append(FileSystem.getCanonicalPath(file, true)).append(" exists already you must specify that you want to overwrite it");
  118.                 return (false);
  119.             }
  120.  
  121.             // jdk 1.1.5 (and higher?) allow you to delete read only files on
  122.             // (WinNT at least)... need to check that it isn't writeable before
  123.             // deleting it
  124.             if((!(file.canWrite())) && (!(file.delete())))
  125.             {
  126.                 errorMsg.append("Unable to delete ").append(FileSystem.getCanonicalPath(file, true));
  127.                 return (false);
  128.             }
  129.         }
  130.         else
  131.         {
  132.             file = new File(file.getParent());
  133.             
  134.             if(!(file.exists()))
  135.             {
  136.                 if(!(file.mkdirs()))
  137.                 {
  138.                     errorMsg.append("Unable to create ").append(FileSystem.getCanonicalPath(file, true));
  139.                     return (false);
  140.                 }
  141.             }
  142.         }        
  143.  
  144.         return (true);
  145.     }
  146.  
  147.     /**
  148.      * @param errorMsg TODO
  149.      * @since VCafe 3.0
  150.      */
  151.  
  152.     protected boolean validateOutputDirectory(StringBuffer errorMsg)
  153.     {
  154.         String dirName;
  155.         File   file;
  156.  
  157.         dirName = options.getOutputLocation();
  158.  
  159.         if(dirName == null)
  160.         {
  161.             errorMsg.append("You must specify an output directory");
  162.             return (false);
  163.         }
  164.  
  165.         file = new File(FileSystem.getCanonicalPath(dirName, true));
  166.  
  167.         if(file.exists())
  168.         {
  169.             if(file.isFile())
  170.             {
  171.                 errorMsg.append(dirName).append(" is a file");
  172.                 return (false);
  173.             }
  174.         }
  175.  
  176.         return (true);
  177.     }
  178.  
  179.     /**
  180.      * @param errorMsg TODO
  181.      * @since VCafe 3.0
  182.      */
  183.  
  184.     protected boolean validateInput(StringBuffer errorMsg)
  185.     {
  186.         if((options.getFiles().length == 0) && (options.getEntries().length == 0))
  187.         {
  188.             errorMsg.append("no files or entries specified");
  189.             return (false);
  190.         }
  191.  
  192.         return (true);
  193.     }
  194.  
  195.     /**
  196.      * @param errorMsg TODO
  197.      * @since VCafe 3.0
  198.      */
  199.  
  200.     protected boolean validateFiles(StringBuffer errorMsg)
  201.     {
  202.         String[] files;
  203.  
  204.         files = options.getFiles();
  205.  
  206.         for(int i = 0; i < files.length; i++)
  207.         {
  208.             if(canceled)
  209.             {
  210.                 return (false);
  211.             }
  212.  
  213.             if(!(new File(files[i]).exists()))
  214.             {
  215.                 errorMsg.append("File ").append(files[i]).append(" does not exist");
  216.                 return (false);
  217.             }
  218.         }
  219.  
  220.         return (true);
  221.     }
  222.  
  223.     /**
  224.      * @param errorMsg TODO
  225.      * @since VCafe 3.0
  226.      */
  227.  
  228.     protected boolean validateEntries(StringBuffer errorMsg)
  229.     {
  230.         String[]  entries;
  231.         Classpath classpath;
  232.  
  233.         entries   = options.getEntries();
  234.         classpath = new Classpath(options.getClasspath());
  235.  
  236.         for(int i = 0; i < entries.length; i++)
  237.         {
  238.             if(canceled)
  239.             {
  240.                 return (false);
  241.             }
  242.  
  243.             if(classpath.find(entries[i]) == null)
  244.             {
  245.                 errorMsg.append("Entry ").append(entries[i]).append(" does not exist");
  246.                 return (false);
  247.             }
  248.         }
  249.  
  250.         return (true);
  251.     }
  252.  
  253.     /**
  254.      * @since VCafe 3.0
  255.      */
  256.  
  257.     public void cancel()
  258.     {
  259.         canceled = true;
  260.     }
  261. }